package com.badlogic.androidgames.glbasics;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.badlogic.androidgames.framework.impl.GLGraphics;

public class FirstTriangleTest extends GLGame{
	
	@Override
	public Screen getStartScreen() {
		return new FirstTriangleScreen(this);
	}
	
	public class FirstTriangleScreen extends Screen{
		GLGraphics glGraphics;
		ByteBuffer byteBuffer;
		FloatBuffer vertices;
		
		public FirstTriangleScreen(Game game){
			super(game);
			glGraphics = ((GLGame) game).getGLGraphics();
			byteBuffer = ByteBuffer.allocateDirect(3 * 2 * 4);
			byteBuffer.order(ByteOrder.nativeOrder());
			vertices = byteBuffer.asFloatBuffer();
			vertices.put( new float[] { 0.0f, 0.0f,
					319.0f, 0.0f,
					160.0f, 479.0f});
			vertices.flip();
		}

		@Override
		public void update(float deltaTime) {
			// TODO Auto-generated method stub
			game.getInput().getTouchEvents();
			game.getInput().getKeyEvents();
			
		}

		@Override
		public void present(float deltaTime) {
			// TODO Auto-generated method stub
			GL10 gl = glGraphics.getGl();
			gl.glClearColor(0,0,0,1);
			gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
			gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
			gl.glMatrixMode(GL10.GL_PROJECTION);
			gl.glLoadIdentity();
			gl.glOrthof(0, 320, 0, 480, 1, -1);
			gl.glColor4f(1, 0, 0, 1);
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
			gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
			
		}

		@Override
		public void pause() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void resume() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub
			
		}
	}

}
